﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;

namespace RunCircle
{
    class TrapCloud : Trap
    {
        List<CCSpriteFrame> m_SafeAnimFrames;
        List<CCSpriteFrame> m_DangerAnimFrames;
        public TrapCloud()
        {
            m_TrapType = "Trap_Cloud";

            m_DangerAnimFrames = new List<CCSpriteFrame>();
            string framename00 = "cloud_0.png";
            CCSpriteFrame spriteframe_00 = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(framename00);
            m_DangerAnimFrames.Add(spriteframe_00);
            string framename01 = "cloud_1.png";
            CCSpriteFrame spriteframe_01 = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(framename01);
            m_DangerAnimFrames.Add(spriteframe_01);
            string framename02 = "cloud_2.png";
            CCSpriteFrame spriteframe_02 = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(framename02);
            m_DangerAnimFrames.Add(spriteframe_02);
            string framename03 = "cloud_3.png";
            CCSpriteFrame spriteframe_03 = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(framename03);
            m_DangerAnimFrames.Add(spriteframe_03);
            string framename04 = "cloud_4.png";
            CCSpriteFrame spriteframe_04 = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(framename04);
            m_DangerAnimFrames.Add(spriteframe_04);

            m_SafeAnimFrames = new List<CCSpriteFrame>();
            m_SafeAnimFrames.Add(spriteframe_00);
            m_SafeAnimFrames.Add(spriteframe_01);
            m_SafeAnimFrames.Add(spriteframe_02);

            initWithSpriteFrameName(framename00);
            CCSpriteFrame frame = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(framename00);
            this.width = frame.Rect.size.width;
            height = frame.Rect.size.height;

            playSafeAnimation();
        }

        public override void setRotateByPoint(CCPoint playerpos, CCPoint anchopos, float rot, CCPoint rotateBy)
        {
            this.position = playerpos;

            if (width > 0.0f && height > 0.0f)
            {
                anchopos.y = anchopos.y - height / 2.0f;
                float ax = 0.5f - (rotateBy.x - anchopos.x) / width;
                float ay = 0.5f - (rotateBy.y - anchopos.y) / height;
                this.anchorPoint = new CCPoint(ax, ay);
            }
            else
            {
                //有问题;
            }

            this.rotation = rot;
        }

        private void playSafeAnimation()
        {
            m_bUsingTrap = false;

            CCAnimation ani = CCAnimation.animationWithFrames(m_SafeAnimFrames);
            CCAnimate animaction = CCAnimate.actionWithDuration(2.0f, ani, true);
            

            List<CCFiniteTimeAction> acs = new List<CCFiniteTimeAction>();
            acs.Add(animaction);

            CCFiniteTimeAction safecallbackAction = CCCallFuncND.actionWithTarget(this,
                                                new SEL_CallFuncND(callbackSafe), this);
            acs.Add(safecallbackAction);
            CCAction safefinal = CCSequence.actions(acs.ToArray());
            this.runAction(safefinal);
        }

        private void callbackSafe(CCNode sender, object data)
        {
            playDangerAnimation();
        }

        private void playDangerAnimation()
        {
            m_bUsingTrap = true;

            CCAnimation ani = CCAnimation.animationWithFrames(m_DangerAnimFrames);
            CCAnimate animaction = CCAnimate.actionWithDuration(2.0f, ani, true);

            List<CCFiniteTimeAction> acs = new List<CCFiniteTimeAction>();
            acs.Add(animaction);

            CCFiniteTimeAction safecallbackAction = CCCallFuncND.actionWithTarget(this,
                                                new SEL_CallFuncND(callbackDanger), this);
            acs.Add(safecallbackAction);
            CCAction safefinal = CCSequence.actions(acs.ToArray());
            this.runAction(safefinal);
        }

        private void callbackDanger(CCNode sender, object data)
        {
            playSafeAnimation();
        }

        public override void update(float dt)
        {
            base.update(dt);
        }

        public override bool checkCollision()
        {
            return false;
        }
    }
}
